package com.example.music;

import com.example.adapter.MusicControl;
import com.xiang.data.Contant;
import com.xiang.utils.GetMp3Data;
import com.xiang.utils.NetUtils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

public class MusicPlayActivity extends Activity implements
		OnClickListener {
	private Button last;
	private Button play;
	private Button next;

	public static SeekBar seekBar = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_play_layout);

		last = (Button) findViewById(R.id.btn_last);
		play = (Button) findViewById(R.id.btn_play);
		next = (Button) findViewById(R.id.btn_next);
		seekBar = (SeekBar) findViewById(R.id.seekbar);

		last.setOnClickListener(this);
		play.setOnClickListener(this);
		next.setOnClickListener(this);

		seekBar.setOnSeekBarChangeListener(new SeekBarChangeEvent());
		/* 退出后再次进去程序时，进度条保持持续更新 */
		if (MusicControl.mMediaPlayer != null) {
			// 设置进度条最大值
			seekBar.setMax(MusicControl.mMediaPlayer.getDuration());
			seekBar.setProgress(MusicControl.mMediaPlayer.getCurrentPosition());
		}
		
		GetMp3Data getMp3Data= new GetMp3Data(this);
		String singerName = getMp3Data.getSingerName(MainActivity.filePath);
		String musicrName = getMp3Data.getMusicName(MainActivity.filePath);
		String musicrAlbum = getMp3Data.getAlbum(MainActivity.filePath);
		final String getlyrUrl = "http://geci.me/api/lyric/"+musicrName;//+"/"+singerName;//获取歌词地址
		
		System.out.println("get :"+musicrName+"/"+singerName+"/"+musicrAlbum);
		new Thread(){
			public void run() {
				try {
					String result = NetUtils.getlrc(getlyrUrl);
					System.out.println("getpic: "+result);
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		}.start();
		
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.btn_last:// 上一曲
			new Thread() {
				public void run() {
					Message msg = new Message();
					msg.what = Contant.MUSIC_LAST;
					MainActivity.handler.sendMessage(msg);
				};
			}.start();
			break;
		case R.id.btn_play:// 播放
			new Thread() {
				public void run() {
					Message msg = new Message();
					msg.what = Contant.MUSIC_PLAY;
					MainActivity.handler.sendMessage(msg);
				};
			}.start();
			break;
		case R.id.btn_next:// 下一曲
			new Thread() {
				public void run() {
					Message msg = new Message();
					msg.what = Contant.MUSIC_NEXT;
					MainActivity.handler.sendMessage(msg);
				};
			}.start();
			break;

		default:
			break;
		}
	}
}

/* 拖放进度监听 ，别忘了Service里面还有个进度条刷新 */
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		/* 假设改变源于用户拖动 */
		if (fromUser) {
			MusicControl.mMediaPlayer.seekTo(progress);// 当进度条的值改变时，音乐播放器从新的位置开始播放
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		MusicControl.mMediaPlayer.pause(); // 开始拖动进度条时，音乐暂停播放
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		MusicControl.mMediaPlayer.start(); // 停止拖动进度条时，音乐开始播放
	}
}
